package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseDao {
  private Connection con = null;
  //构造方法
  public BaseDao(Connection con){this.con=con;}

  /**
   * 通用增删改
   * @param sql
   * @param param
   * @return
   */
  public int executeUpdate(String sql,Object[] param){
    int result = 0;
    PreparedStatement ps=null;
    try {
      ps = con.prepareStatement(sql);
      if(param!=null && param.length!=0){
        for (int i = 0; i < param.length; i++) {
          ps.setObject(i+1,param[i]);
        }
      }
      result = ps.executeUpdate();
    } catch (SQLException throwables) {
      throwables.printStackTrace();
    }
    return result;
  }

  /**
   * 通用查询
   * @param sql
   * @param param
   * @return
   */
  public ResultSet executeQuery(String sql,Object[] param){
    ResultSet rs = null;
    PreparedStatement ps = null;
    try {
      ps = con.prepareStatement(sql);
      if(param!=null && param.length!=0){
        for (int i = 0; i < param.length; i++) {
          ps.setObject(i+1,param[i]);
        }
      }
      rs = ps.executeQuery();
    } catch (SQLException throwables) {
      throwables.printStackTrace();
    }
    return rs;
  }
}
